﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BE;
using BLL;
using UI.GraphService;

namespace UI
{
	
    public partial class Pathfinder : System.Web.UI.Page
    {
		GraphServiceClient client = new GraphServiceClient( "ws" );
        protected void Page_Load(object sender, EventArgs e)
        {
			if ( !IsPostBack )
			{
				fillFromToDdl();
			}
			PrintDirections();
        }

        private void fillFromToDdl()
        {
			ddlFrom.Items.Clear();
			ddlTo.Items.Clear();
				List<Room> rooms = client.GetRooms();
				if ( rooms.Count > 0 )
					foreach ( Room room in rooms )
					{
						ListItem ListRoom = new ListItem(room.Name,""+room.LocationID);
						ddlFrom.Items.Add( ListRoom);
						ddlTo.Items.Add( ListRoom);
					}
				else
				{
					Status.Text = "No Rooms we're found, The system is probably undergoing maintenance or is being set up by the Administrators please be patient";
					Status.ForeColor = System.Drawing.Color.Red;
				}
        }

		protected void CalculatePath( object sender, EventArgs e )
		{
			Room start = new Room(ddlFrom.SelectedItem.Text,Convert.ToInt32(ddlFrom.SelectedItem.Value));
			Room end = new Room(ddlTo.SelectedItem.Text,Convert.ToInt32(ddlTo.SelectedItem.Value));
			List<string> directions = client.getDirections( start, end );
			Session["directions"] = directions;
			Session["start"] = start;
			Session["end"] = end;
			PrintDirections();
		}
		protected void PrintDirections()
		{
			if ( Session["directions"] != null )
			{
				List<string> directions = ( List<string> )Session["directions"];
				PathInfo.Text = "<h1>Direction Instructions for getting from " + ((Room)Session["start"]).Name + " to " + ((Room)Session["end"]).Name + "</h1>";
				foreach ( string d in directions )
					PathInfo.Text += d + "<br />";
			}
			PathInfo.Text += "<br /><img src=\"Image/BuildingFirst.png\" class=\"buildingImage\">";
		}
    }
}